/* eslint-disable */ 
import config from '@/config'
/**对象深拷贝 */
export const deepCopy = function (obj) {
    var newobj = {};
    for (var arr in obj) {
        if (typeof obj[arr] === 'object' && obj[arr] !== null) {
            newobj[arr] = deepCopy(obj[arr]); /*递归，核心代码*/
        } else {
            newobj[arr] = obj[arr];
        }
    }
    return newobj;
}
/**当前时间 */
export const getNowTime = function () {
    let now = new Date();
    let year = now.getFullYear(),
        month = now.getMonth() + 1,
        day = now.getDate(),
        hh = now.getHours(),
        mm = now.getMinutes();
    /*年*/
    let clock = year + "-";
    /*月*/
    (month < 10) ? clock += "0": '';
    clock += month + "-";
    /*日*/
    (day < 10) ? clock += "0": '';
    clock += day + " ";
    /*时*/
    (hh < 10) ? clock += "0": '';
    clock += hh + ":";
    /*分*/
    (mm < 10) ? clock += "0": '';
    clock += mm;

    return (clock);
}
/**时间戳转时间 */
export const changeTime = function (cuo) {
    var now = new Date(cuo);
    return `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`;
}
/**数组拆分 */
export const sliceArray = function (array, size) {
    var result = [];
    for (var x = 0; x < Math.ceil(array.length / size); x++) {
        var start = x * size;
        var end = start + size;
        result.push(array.slice(start, end));
    }
    return result;
}
/**旧版数组合并去重 */
export const arrMergeDifferent = function (arr1, arr2) {
    let _arr = new Array(); /*新建数组*/
    for (var i1 in arr1) {
        /*//初始化新数组为arr1*/
        _arr.push(arr1[i1]);
    }
    for (var i2 in arr2) {
        var flag = true; /*//arr2和arr1的参数状态  true-不一致*/
        for (var i3 in arr1) {
            if (arr2[i2] == arr1[i3]) {
                /*//检查数组1内是否有和当前项一致的参数*/
                flag = false; /*//一致*/
                break;
            }
        }
        if (flag) {
            _arr.push(arr2[i2]); /*//放入*/
        }
    }
    return _arr;
}
/**获取导航栏参数 */
export const getUrlParam = function (name) {
    var data = window.location.search.substr(1).match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"));
    if (data != null) return decodeURI(data[2]);
    return null;
}
/**数字千分符 */
export const numToMoneyField = function (inputString) {
    let regExpInfo = /(\d{1,3})(?=(\d{3})+(?:$|\.))/g;
    let ret = inputString.toFixed(2).toString().replace(regExpInfo, "$1,");
    return ret;
}

export const setObjectOfNull = object => {
    for(var key in object){
        if(typeof object[key] === 'object'){
            object[key] = Array.isArray(object[key]) ? [] : {}
        }else{
            object[key] = ""
        }
    }
}
export const setRandomPath = function(menu){
    for(var curr in menu){
      menu[curr].url ? null : menu[curr].url = '/mypath'+Math.random()+'t'+Math.random()
      menu[curr].subMenus && menu[curr].subMenus.length ? setRandomPath(menu[curr].subMenus) : null
    }
}
  /**选人或部门 */
  export const complexPicker = function(that,obj,func){
    that.getDD(dd => {
        dd.biz.contact.complexPicker({ 
            title: "请选择", //标题
            corpId:localStorage.corpId, //企业的 corpid
            multiple: true, //是否多选
            limitTips: "超出了", //超过限定人数返回提示
            maxUsers: obj.maxUsers||1000000, //最大可选人数
            pickedUsers:obj.useridArr||[], //已选用户
            disabledUsers:obj.disabledUsers||[],            //不可选用户
            appId:config['appId'], //微应用的Id
            permissionType: "GLOBAL", //可添加权限校验，选人权限，目前只有GLOBAL这个参数
            responseUserOnly:obj.bool||false, //返回人，或者返回人和部门
            startWithDepartmentId: 0, //仅支持0和-1
            onSuccess: result => {
                log('result',result)
                result.dept_id = ''
                result.user_id = ''
                result.userArr = ''
                
                if(result.users&&result.users.length){ /**处理表单用户ids */
                    let arr = []
                    let userArr = []
                    result.users.forEach(item=>{
                      arr.push(item.emplId)
                      userArr.push(item.name)
                    })
                    result.user_id = arr.join(',')
                    result.userArr = userArr.join(',')
                  }
                  if(result.departments&&result.departments.length){/**处理表单部门ids */
                    let arr = []
                    result.departments.forEach(item=>{
                      arr.push(item.id)
                    })
                    result.dept_id = arr.join(',')
                  }
                func(result)
            },
            onFail: function(err) {
                console.log('err',err)}
        });
    })
}
  /**选部门 */
  export const departmentsPicker = function(that,obj,func){
    that.getDD(dd => {
        dd.biz.contact.departmentsPicker({
            title:"选择部门",            //标题
            corpId:localStorage.corpId,    //企业的 corpid
            multiple:true,            //是否多选
            limitTips:"超出了",          //超过限定人数返回提示
            maxDepartments:obj.maxDept||100000,            //最大选择部门数量
            pickedDepartments:obj.pickedDepartments||[],//已选部门
            disabledDepartments:obj.disDept||[],        //不可选部门
            requiredDepartments:[],        //必选部门（不可取消选中状态）
            appId:config['appId'],              //微应用的Id
            permissionType:"GLOBAL",   
            onSuccess: result => {
                result.dept_id = ''
                result.deptArr = ''
                if(result.departments&&result.departments.length){/**处理表单部门ids */
                    let arr = []
                    let nameArr = []
                    console.log('result')
                    result.departments.forEach(item=>{
                        arr.push(item.id)
                        nameArr.push(item.name)
                    })
                    result.dept_id = arr.join(',')
                    result.deptArr = nameArr.join(',')
                }
                // if(result.dept_id==-1){
                //     result.dept_id = ''
                //     result.deptArr = ''
                //     Toast('不能选顶级部门')
                // }
                func(result)
            },
            onFail: function(err) {console.log(err)}
        });
    })
}